<?php

namespace app\sales\controller;

use app\admin\common\Base;
use think\Db;
//往来单位欠款表
class Contactdebtnew extends Base
{
   public function index()
   {
      $data = $this->request->param();
      if (isset($data['ajax']) && $data['ajax'] == 100) {
         $map = '';
         $page = isset($data['page']) ? $data['page'] : 1;
         $page = intval($page);
         $limit = isset($data['rows']) ? $data['rows'] : 10;
         $limit = intval($limit);
         $start = $limit * ($page - 1);


         if ($map == '') {
            $list = Db::name('customer')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
            $count = Db::name('customer')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
         } else {
            $list = Db::name('customer')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
            $count = Db::name('customer')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
         }
         foreach ($list as $key => $value) {
            // 应付和应收
            if ($value['is_customer'] == 0 || $value['is_customer'] == 3) {
               $list[$key]['ys'] = user_arrears($value['id']);
            }
            else{
               $list[$key]['yf'] = user_meet($value['id']);
            }

            // 完善数据
            $list[$key]['is_customer'] = $value['is_customer'] == 0 ? '客户' : '供应商';
         }
         return $this->grid_data($list, $count, $page, $limit);
      }

      return $this->fetch();
   }


   public function add()
   {
      $data = $this->request->param();


      return $this->fetch();
   }
}
